package com.lagou.controller;

import com.lagou.domain.*;
import com.lagou.service.MenuService;
import com.lagou.service.ResourceCategoryService;
import com.lagou.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private ResourceCategoryService resourceCategoryService;

    /**
     * 角色列表查询&条件查询
     *
     * @param role
     * @return
     */
    @RequestMapping("/findAllRole")
    public ResponseResult findAllRole(@RequestBody Role role) {

        List<Role> list = roleService.findAllRole(role);
        return new ResponseResult(true, 200, "角色列表查询&条件查询", list);
    }

    /**
     * 添加&修改角色
     *
     * @param role
     * @return
     */
    @RequestMapping("/saveOrUpdateRole")
    public ResponseResult saveOrUpdateRole(@RequestBody Role role) {

        String msg = null;
        if (role.getId() == null) {

            roleService.saveRole(role);
            msg = "添加角色信息";
        } else {

            roleService.updateRole(role);
            msg = "修改角色信息";
        }

        return new ResponseResult(true, 200, msg, null);
    }

    /**
     * 查询所有菜单列表
     *
     * @return
     */
    @RequestMapping("/findAllMenu")
    public ResponseResult findAllMenu() {

        List<Menu> list = menuService.findSubMenuListByPid(-1);
        Map<String, Object> map = new HashMap<>();
        map.put("parentMenuList", list);
        return new ResponseResult(true, 200, "查询所有菜单列表", map);
    }

    /**
     * 根据角色ID查询关联菜单ID
     *
     * @param roleId 角色id
     * @return
     */
    @RequestMapping("/findMenuByRoleId")
    public ResponseResult findMenuByRoleId(Integer roleId) {

        List<Integer> list = menuService.findMenuByRoleId(roleId);
        return new ResponseResult(true, 200, "根据角色ID查询关联菜单ID", list);
    }

    /**
     * 为角色分配菜单列表
     *
     * @param roleMenuVO
     * @return
     */
    @RequestMapping("/RoleContextMenu")
    public ResponseResult roleContextMenu(@RequestBody RoleMenuVO roleMenuVO) {

        roleService.roleContextMenu(roleMenuVO);
        return new ResponseResult(true, 200, "为角色分配菜单列表", null);
    }

    /**
     * 删除角色
     *
     * @param id 角色id
     * @return
     */
    @RequestMapping("/deleteRole")
    public ResponseResult deleteRole(Integer id) {

        roleService.deleteRole(id);
        return new ResponseResult(true, 200, "删除角色", null);
    }

    /**
     * 获取当前角色拥有的 资源信息
     *
     * @param roleId 角色id
     * @return
     */
    @RequestMapping("/findResourceListByRoleId")
    public ResponseResult findResourceListByRoleId(Integer roleId) {

        List<ResourceCategory> list = resourceCategoryService.findResourceListByRoleId(roleId);
        return new ResponseResult(true, 200, "获取当前角色拥有的 资源信息", list);
    }

    /**
     * 为角色分配资源
     * @param roleResourceVO
     * @return
     */
    @RequestMapping("/roleContextResource")
    public ResponseResult roleContextResource(@RequestBody RoleResourceVO roleResourceVO) {

        roleService.roleContextResource(roleResourceVO);
        return new ResponseResult(true, 200, "为角色分配资源", null);
    }
}
